package org.example.votingsystem.util;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SqlManege {
    //    建立连接的方法
//    第一次使用时  只执行一次
    static {
    try {
        DriverManager.registerDriver(new Driver());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
    public Connection connect() throws SQLException {
//    注册驱动

        String url = "jdbc:mysql://127.0.0.1:3306/voting_system?serverTimezone=Asia/Shanghai";
        String user = "root";
        String password = "root";
        //---------------------------------------------------
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);

        return connection;
    }

    //    断开连接
    public void disconnect(Connection c, PreparedStatement p) throws SQLException {
        c.close();
        p.close();
    }
}
